home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / mr.c < prev    next >
C/C++ Source or Header  |  1994-10-02  |  7KB  |  375 lines

  1. /**
  2.   Citadel Mail Snooper Utility Version 1.00
  3. **/
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8.  /*
  9.   * #define DEBUG
  10.   */
  11.  
  12. void      StopDecode(void);
  13. void      Process(char *file);
  14. void GetStr(FILE * fd, char *buf);
  15. void StartDecode( int (*func)() );
  16. int  Decode(int c);
  17. int moo(int c);
  18. int Decode2(int val);
  19. #define TRUE    1
  20. #define FALSE   0
  21.  
  22. int main(int, char **);
  23.  
  24. int main(int argc, char **argv)
  25.   {
  26.   FILE *fp;
  27.   char line[80];
  28.   int i, j, k;
  29.   fprintf(stderr," Citadel Mail Identifier Version 1.00\n");
  30.   if( argc > 1 )
  31.     {
  32.     for( i=1; i<argc; i++)Process(argv[i]);
  33.     }
  34.   else
  35.     {
  36.   if( (fp=fopen("map.sys", "r")) == NULL )
  37.     {
  38.     fprintf(stderr,"unable to open map.sys\n");
  39.     fprintf(stderr,"This utility must be run from the DOMAINAREA\n");
  40.     }
  41.   else
  42.     {
  43.     while( fgets(line,80,fp) )
  44.       {
  45.       i = atoi(line);
  46.       for( j=2; line[j] != ' ' && j < 80; j++);
  47.       for(    ; line[j] == ' ' && j < 80; j++);  /* find start of number */
  48.       j = atoi(&line[j]);
  49.       for(k=0; k<j; k++)
  50.         {
  51.         sprintf(line,"%d/%d",i,k);
  52.         Process(line);
  53.         };
  54.       };
  55.     fclose(fp);
  56.     };
  57.   };
  58.   return 0;
  59.   }
  60. void strip(char *p)
  61.   {
  62.   int i = strlen(p);
  63.   for(i--; i> 0 && (p[i] == ' '|| p[i]=='\0'); i--);
  64.   p[i+1]='\0';
  65.   }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. char      author[50];
  72. char        date[50];
  73. char        node[50];
  74. char          to[50];
  75. char      domain[50];
  76.  
  77. #define STARTING        0
  78. #define COLLECTING        1
  79. #define EATING                2
  80. int       state = STARTING;
  81. void Process(char *file)
  82. {
  83.   char      id[21];
  84.   int       c;
  85.   char      name[21];
  86.   FILE     *fd;
  87.  
  88.   author[0] = 0;
  89.   date[0] = 0;
  90.   to[0] = 0;
  91.   node[0] = 0;
  92.   domain[0] = 0;
  93.   state = STARTING;
  94.   if ((fd = fopen(file, "rb")) == NULL)return;
  95.   GetStr(fd, id);
  96.   GetStr(fd, name);
  97.  
  98.   StartDecode(moo);
  99.   while ((c = getc(fd)) != EOF)
  100.     {
  101.       Decode(c);
  102.     }
  103.   StopDecode();
  104.   fclose(fd);
  105.   strip(author);
  106.   strip(date);
  107.   strip(to);
  108.   strip(node);
  109.   strip(domain);
  110.   printf("%s from %s@%s_%s to %s@%s\n", date, author, node,domain, to,
  111.      name);
  112. }
  113.  
  114. void GetStr(FILE * fd, char *buf)
  115. {
  116.   int       c,
  117.             i = 0;
  118.  
  119.   while ((c = getc(fd)) != 0 && c != EOF)
  120.     {
  121.       buf[i++] = c;
  122.     }
  123.   buf[i] = 0;
  124. }
  125.  
  126. int moo(c)
  127.   int       c;
  128. {
  129.   static int ind;
  130.   static char *str;
  131.  
  132.   if (state == STARTING)
  133.     {
  134.       ind = 0;
  135.       switch (c)
  136.     {
  137.       case 'A':
  138.         str = author;
  139.         state = COLLECTING;
  140.         break;
  141.       case 'D':
  142.         str = date;
  143.         state = COLLECTING;
  144.         break;
  145.       case 'T':
  146.         str = to;
  147.         state = COLLECTING;
  148.         break;
  149.       case 'N':
  150.         str = node;
  151.         state = COLLECTING;
  152.         break;
  153.       case 'X':
  154.         str = domain;
  155.         state = COLLECTING;
  156.         break;
  157.       default:
  158.         state = EATING;
  159.     }
  160.     }
  161.   else if (state == EATING)
  162.     {
  163.       if (c == 0)
  164.     {
  165.       state = STARTING;
  166.     }
  167.     }
  168.   else
  169.     {
  170.       if (c == 0)
  171.     {
  172.       state = STARTING;
  173.     }
  174.       str[ind++] = c;
  175.     }
  176.   return TRUE;
  177. }
  178.  
  179. #define CAPS            0
  180. #define NO_CAPS         1
  181. #define NUMERICS        2
  182. #define SPEC_CHARS      3
  183. #define FINISHED        4    /*
  184.                  * finished with encoded data
  185.                  */
  186. #define NO_SET          5    /*
  187.                  * an initial value
  188.                  */
  189.  
  190. #define COMP_SPACE      26    /*
  191.                  * universal
  192.                  */
  193. #define ZERO_COMP       16
  194. #define RET_COMP        17
  195.  
  196. #ifdef NEEDED
  197.  
  198. struct
  199. {
  200.   char      Set,          /*
  201.                    * code set
  202.                    */
  203.             CompValue;          /*
  204.                    * position in code set
  205.                    */
  206. }
  207. compress[] =
  208. {
  209.   { 0, 0 }, { 2, 10 }, { 2, 11 }, { 2, 12 }, { 2, 13 },    /*     * ' ' - '$'      */
  210.   { 2, 14 }, { 2, 15 }, { 2, 16 }, { 2, 17 }, { 2, 18 },    /*     * '%' - ')'      */
  211.   { 2, 19 }, { 2, 20 }, { 2, 21 }, { 2, 22 }, { 2, 23 },    /*     * '*' - '.'      */
  212.   { 2, 24 }, { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 },    /*     * '/' - '3'      */
  213.   { 2, 4 }, { 2, 5 }, { 2, 6 }, { 2, 7 }, { 2, 8 },    /*     * '4' - '8'      */
  214.   { 2, 9 }, { 2, 25 }, { 3, 0 }, { 3, 1 }, { 3, 2 },    /*     * '9' - '='      */
  215.   { 3, 3 }, { 3, 4 }, { 3, 5 }, { 0, 0 }, { 0, 1 },    /*     * '>' - 'B'      */
  216.   { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 6 },    /*     * 'C' - 'G'      */
  217.   { 0, 7 }, { 0, 8 }, { 0, 9 }, { 0, 10 }, { 0, 11 },    /*     * 'H' - 'L'      */
  218.   { 0, 12 }, { 0, 13 }, { 0, 14 }, { 0, 15 }, { 0, 16 },    /*     * 'M' - 'Q'      */
  219.   { 0, 17 }, { 0, 18 }, { 0, 19 }, { 0, 20 }, { 0, 21 },    /*     * 'R' - 'V'      */
  220.   { 0, 22 }, { 0, 23 }, { 0, 24 }, { 0, 25 }, { 3, 6 },    /*     * 'W' - '['      */
  221.   { 3, 7 }, { 3, 8 }, { 3, 9 }, { 3, 10 }, { 3, 11 },    /*     * '\' - '`'      */
  222.   { 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 },    /*     * 'a' - 'e'      */
  223.   { 1, 5 }, { 1, 6 }, { 1, 7 }, { 1, 8 }, { 1, 9 },    /*     * 'f' - 'i'      */
  224.   { 1, 10 }, { 1, 11 }, { 1, 12 }, { 1, 13 }, { 1, 14 },    /*     * 'j' - 'o'      */
  225.   { 1, 15 }, { 1, 16 }, { 1, 17 }, { 1, 18 }, { 1, 19 },    /*     * 'p' - 't'      */
  226.   { 1, 20 }, { 1, 21 }, { 1, 22 }, { 1, 23 }, { 1, 24 },    /*     * 'u' - 'y'      */
  227.   { 1, 25 }, { 3, 12 }, { 3, 13 }, { 3, 14 }, { 3, 15 }    /** 'z' - ''      */};
  228.  
  229. #endif
  230.  
  231. static char Numerics[] =
  232. {
  233.   '0', '1', '2', '3', '4',
  234.   '5', '6', '7', '8', '9',
  235.   '!', '\"', '#', '$', '%',
  236.   '&', '\'', '(', ')', '*',
  237.   '+', ',', '-', '.', '/',
  238.   ':'
  239. };
  240.  
  241. static char SpecChars[] =
  242. {
  243.   ';', '<', '=', '>', '?',
  244.   '@', '[', '\\', ']', 0x5e,
  245.   '_', '`', '{', '|', '}',
  246.   ' ', 0, '\r'
  247. };
  248.  
  249. static char CurSet;
  250. static int OB,
  251.           ThisVal,
  252.           NB;
  253. int       (*OutFunc) ();
  254.  
  255.  
  256. void StartDecode(func)
  257.   int       (*func) ();
  258. {
  259.   ThisVal = 0;
  260.   CurSet = NO_SET;
  261.   OutFunc = func;
  262.   NB = 5;
  263. }
  264.  
  265. void
  266. StopDecode()
  267. {
  268. }
  269.  
  270. int
  271. Decode(c)
  272.   int       c;
  273. {
  274.   static int mask[] =
  275.   {
  276.     0, 1, 3, 7, 0xf, 0x1f
  277.   };
  278.   int       AB;
  279.  
  280.   if (CurSet == FINISHED)
  281.     return TRUE;
  282.  
  283. #ifdef DEBUG
  284.   fprintf(stderr, "decoding %x, ThisVal is currently %x\n", c, ThisVal);
  285. #endif
  286.  
  287.   if (!Decode2(c & mask[NB]))
  288.     return FALSE;
  289.  
  290.   c >>= NB;
  291.  
  292. #ifdef DEBUG
  293.   fprintf(stderr, "c becomes %x\n", c);
  294. #endif
  295.   AB = 8 - NB;
  296.  
  297.   if (AB >= 5)
  298.     {
  299.       ThisVal = 0;
  300.       AB -= 5;
  301.       NB = 5;
  302.       if (!Decode2(c & mask[NB]))
  303.     return FALSE;
  304.       c >>= 5;
  305. #ifdef DEBUG
  306.       fprintf(stderr, "c becomes %x\n", c);
  307. #endif
  308.     }
  309.   NB = 5 - AB;
  310.   ThisVal = c;
  311.   return TRUE;
  312. }
  313.  
  314. int Decode2(val)
  315.   int       val;
  316. {
  317.   int       toReturn;
  318.   char      Used;
  319.  
  320.   if (CurSet == FINISHED)
  321.     return TRUE;
  322.   ThisVal += (val << (5 - NB));
  323. #ifdef DEBUG
  324.   fprintf(stderr, "ThisVal calculates to be %x, val is %x\n", ThisVal, val);
  325. #endif
  326.   switch (ThisVal - 27)
  327.     {
  328.       case FINISHED:
  329. #ifdef DEBUG
  330.     fprintf(stderr, "Finished found.\n");
  331. #endif
  332.       case CAPS:
  333.       case NO_CAPS:
  334.       case NUMERICS:
  335.       case SPEC_CHARS:
  336.     CurSet = ThisVal - 27;
  337.     Used = TRUE;
  338.     toReturn = TRUE;
  339.     break;
  340.       default:
  341.     Used = FALSE;
  342.     }
  343. /*
  344.  * Discard if no set defined.
  345.  */
  346.   if (!Used)
  347.     {
  348.       if (CurSet == NO_SET)
  349.     toReturn = TRUE;
  350.  
  351.  /*
  352.   * True for any valid code set
  353.   */
  354.       else if (ThisVal == COMP_SPACE)
  355.     toReturn = (*OutFunc) (' ');
  356.       else
  357.     switch (CurSet)
  358.       {
  359.         case CAPS:
  360.           toReturn = (*OutFunc) ('A' + ThisVal);
  361.           break;
  362.         case NO_CAPS:
  363.           toReturn = (*OutFunc) ('a' + ThisVal);
  364.           break;
  365.         case NUMERICS:
  366.           toReturn = (*OutFunc) (Numerics[ThisVal]);
  367.           break;
  368.         case SPEC_CHARS:
  369.           toReturn = (*OutFunc) (SpecChars[ThisVal]);
  370.           break;
  371.       }
  372.     }
  373.   return toReturn;
  374. }
  375.